package com.e.dianshang.controller;

import com.e.dianshang.service.HotSearchService;
import com.e.dianshang.service.IndexService;
import com.e.dianshang.utils.BusinessException;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date  2020/11/11 14:18
 */
@RestController

@Api(value = "首页控制器",tags = "首页接口的api")
public class IndexController {
    @Resource
    IndexService indexService;
    @Resource
    HotSearchService hotSearchService;
    /**
     *  显示默认搜索商品的接口
     * @param openId 当前用户的电话号码
     * @return 默认搜索的商品
     */
    @PostMapping("/defaultSearch")
    @ApiOperation(value = "显示默认搜索的接口")
    @ApiImplicitParam(name = "openid",value = "当前用户的电话号码或者微信id",required = true,
            dataType = "String",paramType = "query")
    public Map defaultSearch(@RequestParam("openid") String openId) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if(openId !=null && !openId.equals("")){
            String good = indexService.defaultSearch(openId);
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
            jsonResult.put("goods",good);
        }else{
            throw new BusinessException("电话号码为空！");
        }
        return jsonResult.getValues();
    }
    /**
     *  搜索商品
     * @param goods 商品或者品牌
     * @return 商品信息
     * @throws BusinessException
     */
    @RequestMapping("/search")
    @ApiOperation(value = "搜索商品的接口")
    @ApiImplicitParam(name = "goods",value = "输入搜索商品或者品牌",required = true,
            dataType = "String",paramType = "query")
    public Map search(@RequestParam("goods") String goods) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        String  ZSET = "topSearch";
        if(goods != null && !goods.equals("")){
            List<Map> search = indexService.search(goods);
            Long rank2 = hotSearchService.rank(ZSET, goods);
            if(null == rank2){
                hotSearchService.zAdd(ZSET,goods,1.0);
            }else {
                int score = (int)hotSearchService.score(ZSET, goods);
                hotSearchService.zAdd(ZSET,goods,score+1);
            }
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
            jsonResult.put("goodsList",search);
        }else{
            throw new BusinessException("输入的商品或者品牌为空！！！");
        }
        return jsonResult.getValues();
    }
    @PostMapping("/recommend")
    @ApiOperation(value = "推荐商品的接口")
    @ApiImplicitParam(name = "phone",value = "当前用户的电话号码或者微信id",required = true,
            dataType = "String",paramType = "query")
    public Map recommendGoods(@RequestParam("phone") String phone) {
        List<Map> list = indexService.recommendGoods(phone);
        JsonResult jsonResult = new JsonResult();
        if(!list.isEmpty()){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
            jsonResult.put("goods",list);
        }
        return jsonResult.getValues();
    }
    @PostMapping("/timeUpGoods")
    @ApiOperation(value = "秒杀商品的接口")
    public Map timeUpGoods(){
        List<Map> list = indexService.timeUpGoods();
        JsonResult jsonResult = new JsonResult();
        if(!list.isEmpty()){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
            jsonResult.put("goods",list);
        }
        return jsonResult.getValues();
    }
}
